package rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Parametri;

@Stateless
@Local(ParametriDao.class)
public class ParametriDaoBean implements ParametriDao {

	@PersistenceContext(unitName = "etaksi")
	protected EntityManager em;

	@Override
	public Parametri tekuci() {
		Parametri parametri = null;
		try {
			Query upit = em.createNamedQuery("radniParametri");
			parametri = (Parametri) upit.getSingleResult();
		} catch (NoResultException ex) {
			// TODO obraditi izuzetak
		}
		return parametri;
	}

	@Override
	public Parametri sacuvaj(Parametri novi) {
		em.persist(novi);
		return novi;
	}

	@Override
	public Parametri stopi(Parametri tekuci) {
		tekuci = em.merge(tekuci);
		return tekuci;
	}

	@Override
	public void ukloni(Parametri tekuci) {
		tekuci = em.merge(tekuci);
		em.remove(tekuci);
	}

	@Override
	public void posalji() {
		em.flush();
	}

	@Override
	public void ocisti() {
		em.clear();
	}

}
